package cn.com.surker.base.strength.service.impl;

import cn.com.surker.base.strength.entity.BaseStrengthLevelDosage;
import cn.com.surker.base.strength.dao.BaseStrengthLevelDosageDao;
import cn.com.surker.base.strength.service.BaseStrengthLevelDosageService;
import cn.com.surker.base.strength.vo.StrengthLevelDosageVo;
import cn.com.surker.common.MessageCenterException;
import cn.com.surker.util.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import cn.com.surker.spring.RestResultDto;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
* <p>
    * 强度等级容重区间表 服务实现类
    * </p>
*
* @author admin
* @since 2025-01-13
*/
@Service
public class BaseStrengthLevelDosageServiceImpl extends ServiceImpl<BaseStrengthLevelDosageDao, BaseStrengthLevelDosage> implements BaseStrengthLevelDosageService {

    @Resource
    private BaseStrengthLevelDosageDao dosageDao;

    /**
     * 查询基础标号容重区间值
     * @param orgId
     * @param type
     * @return
     */
    public List<StrengthLevelDosageVo> findDosageVo(Long orgId, Integer type) {
        if (StringUtils.isNull(orgId))
            return null;
        if (StringUtils.isNull(type))
            type = null;
        List<StrengthLevelDosageVo> list = dosageDao.findDosageVo(orgId, type);
        if (list == null || list.size() == 0)
            return null;
        return list;
    }

    /**
     * 新增容重区间信息
     * @param dosage
     * @return
     */
    public RestResultDto saveDosage(BaseStrengthLevelDosage dosage) {
        if (dosage == null)
            throw new MessageCenterException("参数错误");
        if (StringUtils.isNull(dosage.getMinDosage()) || dosage.getMinDosage() < 500)
            dosage.setMinDosage(2300.0);
        if (StringUtils.isNull(dosage.getMaxDosage()) || dosage.getMaxDosage() < 500)
            dosage.setMaxDosage(2400.0);
        BaseStrengthLevelDosage oldDosage = dosageDao.findExistDosage(dosage.getOrgId(), dosage.getStrengthId());
        if (oldDosage != null)
            throw new MessageCenterException("存在同样标号的容重区间设置");
        dosage.setCreateTime(new Date());
        Integer code = dosageDao.insert(dosage);
        if (code > 0)
            return RestResultDto.success("新增成功", dosage);

        throw new MessageCenterException("新增失败");
    }

    /**
     * 更新容重区间信息
     * @param dosage
     * @return
     */
    public RestResultDto updateDosage(BaseStrengthLevelDosage dosage) {
        if (dosage == null || StringUtils.isNull(dosage.getId()))
            throw new MessageCenterException("参数错误");
        BaseStrengthLevelDosage oldDosage = dosageDao.selectById(dosage.getId());
        if (oldDosage == null || oldDosage.getStatus() == 1)
            throw new MessageCenterException("容重区间不存在或已被删除");
        dosage.setUpdateTime(new Date());
        Integer code = dosageDao.updateById(dosage);
        if (code > 0)
            return RestResultDto.success("更新成功", dosage);

        throw new MessageCenterException("更新失败");
    }

    /**
     * 更新状态
     * @param id
     * @param status
     * @param creator
     * @return
     */
    public RestResultDto updateStatus(Long id, Integer status, String creator) {
        if (StringUtils.isAllParamNull(id, status))
            throw new MessageCenterException("参数错误");

        BaseStrengthLevelDosage dosage = dosageDao.selectById(id);
        if (dosage == null)
            throw new MessageCenterException("容重区间不存在");
        dosage.setUpdator(creator);
        dosage.setUpdateTime(new Date());
        dosage.setStatus(status);
        Integer code = dosageDao.insert(dosage);
        if (code > 0)
            return RestResultDto.success("更新成功", dosage);

        throw new MessageCenterException("更新失败");
    }

    public RestResultDto saveDosageBatch(List<BaseStrengthLevelDosage> list) {
        if (list == null || list.size() == 0)
            throw new MessageCenterException("参数错误");
        String message = "";
        Date nowDate = new Date();
        List<BaseStrengthLevelDosage> tempList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            BaseStrengthLevelDosage dosage = list.get(i);
            BaseStrengthLevelDosage oldDosage = dosageDao.findExistDosage(dosage.getOrgId(), dosage.getStrengthId());
            if (oldDosage != null) {
                message += oldDosage.getLevel() + ",";
                continue;
            }
            dosage.setCreateTime(nowDate);
            dosage.setStatus(0);
            dosage.setRemark("复制基础强度等级新增容重区间设置");
            tempList.add(dosage);
        }
        Integer code = dosageDao.saveBatch(tempList);
        if (StringUtils.isNull(message))
            message = "保存成功";
        if (code > 0)
            return RestResultDto.success(message);

        throw new MessageCenterException("保存失败");
    }
}